//
//  MyselfViewController.m
//  Liuhe
//
//  Created by zr on 2018/8/7.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "MyselfViewController.h"
#import "MyselfTableViewCell.h"
#import "MyselfHeaderView.h"
#import "MyselfInfoViewController.h"
#import "MyBoomViewController.h"
#import "MyMessageViewController.h"
#import "MyArticleViewController.h"

@interface MyselfViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSArray * imageNameArray;
@property (nonatomic, strong)NSArray * showStringArray;
@property (nonatomic, strong)MyselfHeaderView * header;
@property (nonatomic, copy)NSURL * url;
@property (nonatomic, copy)NSString * name;
@property (nonatomic, copy)NSString * adress;
@property (nonatomic, copy)NSString * person;
@property (nonatomic, copy)NSString * sex;

@end

@implementation MyselfViewController


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    
    NSString * token = DEF_GET_OBJECT(@"token");
    
    NSDictionary * dict = @{@"token":token};
    
    [[NetworkTool sharedTools] POST:@"/index/Member/info" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        int status = [responseObject[@"status"] intValue];
        
        if (status == 200) {
            
            NSDictionary * dict = responseObject[@"data"];
            
            self.header.nameString = dict[@"username"];
            
            NSURL * url = [NSURL URLWithString:dict[@"headimg"]];
            self.header.url = url;
            self.url = url;
            
            self.header.cityString = dict[@"city"];
            self.header.birString = dict[@"birthday"];
            self.name = dict[@"username"];
            self.adress = dict[@"address"];
            self.person = dict[@"signature"];
            self.sex = dict[@"sex"];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    
    self.showStringArray = @[@"我的信息",@"我的文章",@"我的爆料",@"个人信息"];
    self.imageNameArray = @[@"消息",@"文章",@"爆款",@"个人"];
    
    [self setupUI];
    
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 65 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.scrollEnabled = NO;
    if (@available(iOS 11.0, *)) {
        tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[MyselfTableViewCell class] forCellReuseIdentifier:@"MyselfListCell"];
    [self.tableView registerClass:[MyselfHeaderView class] forHeaderFooterViewReuseIdentifier:@"MyselfHeaderView"];
    
    [self.view addSubview:self.tableView];
    
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyselfTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyselfListCell" forIndexPath:indexPath];
    
    cell.titleLabel.text = self.showStringArray[indexPath.row];
    cell.iconImageView.image = ImageNamed(self.imageNameArray[indexPath.row]);
    
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 200 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    self.header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MyselfHeaderView"];
    
    return self.header;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    if (indexPath.row == 3) {
        
        MyselfInfoViewController * infoVC = [MyselfInfoViewController new];
        
        infoVC.name = self.name;
        infoVC.gender = self.sex;
        infoVC.adress = self.adress;
        infoVC.person = self.person;
        infoVC.url = self.url;
        
        [self.navigationController pushViewController:infoVC animated:YES];
    }else if (indexPath.row == 2){
        
        MyBoomViewController * boomVC = [MyBoomViewController new];
        
        [self.navigationController pushViewController:boomVC animated:YES];
    }else if (indexPath.row == 0){
        
        MyMessageViewController * msgVC = [MyMessageViewController new];
        
        [self.navigationController pushViewController:msgVC animated:YES];
    }else if (indexPath.row == 1){
        
        MyArticleViewController * artVC = [MyArticleViewController new];
        
        [self.navigationController pushViewController:artVC animated:YES];
    }
    
    SHOW_TAB;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
